#!/usr/bin/env python
#-*- coding:UTF-8 -*-
#####################################################
# Author: Jiaion - jiaion21@gmail.com
# Last modified:	2011-04-23 16:14
# Filename:	system.py
#####################################################
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from Context.Render import render
from django.contrib.auth.decorators import login_required
from django.conf import settings
import Jredis as redis
import sys

def ExecReturn(request):
    SID = request.REQUEST.get('SID')
    try :
        FILE = '%s%s.log' % (settings.LOGDIR,SID)
        TEMP = open(FILE,'r')
        TXTAREA = TEMP.read()
        TEMP.close()
    except :
        TXTAREA = 'empty'
        pass
    return HttpResponse(TXTAREA) 

@login_required()
def System(request):
    return render('system.html',{},request)

@login_required()
def Webssh(request):
    return render('webssh.html',{},request)

@login_required()
def Batch(request):
    import Fabric
    Exec = Fabric
    AllCommand = Exec.__ALL__
    if request.method == 'POST':
        ExecCommand = request.REQUEST.get('Exec')
        HOSTS = request.REQUEST.get('Hosts','').split()
        SID = request.REQUEST.get('SID')
        if ExecCommand and HOSTS:
            value = {'SID':SID, 'HOSTS':HOSTS, 'COMMAND':ExecCommand,}
            redis.lpushs('CommQueue',value)
            return HttpResponse('OK\n')
        return render('batch.html',{'COMM':AllCommand,},request)
    return render('batch.html',{'COMM':AllCommand,},request)

